package com.zl.tby.screen.controller;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyWeatherQuery;
import com.zl.tby.screen.service.ITbyEcologyInfoScreenService;
import com.zl.tby.screen.service.ITbyForestScreenService;
import com.zl.tby.screen.vo.ecology.TbyEcologyScreenInfo;
import com.zl.tby.screen.vo.forest.TbyForestScreenBean;
import com.zl.tby.service.ITbyWeatherService;
import com.zl.tby.vo.TbyWeatherForm;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.MethodType;
import org.noear.solon.validation.annotation.Valid;

import java.util.*;

/**
 * 火情监测大屏接口
 * @author changpeng
 * @version 1.0
 * @since 2024/8/19 10:18
 */
@Valid
@Controller
@Mapping("/forest")
public class TbyForestScreenController {
    @Inject
    private ITbyForestScreenService tbyForestScreenService;
    @Inject
    private ITbyWeatherService tbyWeatherService;
    @Inject
    private ITbyEcologyInfoScreenService tbyEcologyInfoScreenService;

    /**
     * 获取火险等级及无火灾天数
     * @return 结果
     */
    @Mapping(value = "/config", method = MethodType.GET)
    public R<TbyForestScreenBean> config() {
        return R.ok(this.tbyForestScreenService.getConfig());
    }

    /**
     * 天气历史
     * @param query: 查询天气历史
     * @return 结果
     */
    @Mapping(value = "/weather_chart_data", method = MethodType.GET)
    public R<List<TbyWeatherForm>> weatherChartData(TbyWeatherQuery query) {
        List<String> dateList = new ArrayList<>();
        if (query.getType() != null && query.getType().equals("history")) {
            int day = query.getDay() == null ? 5 : query.getDay();
            Date current = ToolsUtil.dateString2Util(ToolsUtil.dateUtil2String(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            dateList.add(ToolsUtil.dateUtil2String(current, "yyyy-MM-dd") + " 00:00:00");
            for (int i = 1; i < day; i++) {
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.YEAR, -(i));
                dateList.add(ToolsUtil.dateUtil2String(calendar.getTime(), "yyyy-MM-dd") + " 00:00:00");
            }
        } else if (query.getType() != null && query.getType().equals("scope")) {
            int day = query.getDay() == null ? 7 : query.getDay();
            Date current = ToolsUtil.dateString2Util(ToolsUtil.dateUtil2String(new Date(System.currentTimeMillis()), "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");
            for (int i = day; i > 0; i--) {
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DATE, -(i));
                dateList.add(ToolsUtil.dateUtil2String(calendar.getTime(), "yyyy-MM-dd") + " 00:00:00");
            }
            dateList.add(ToolsUtil.dateUtil2String(current, "yyyy-MM-dd") + " 00:00:00");
        }
        query.setDateArray(dateList);
        List<TbyWeatherForm> dataList = this.tbyWeatherService.list(query);
        Map<String, TbyWeatherForm> map = new HashMap<>(0);
        dataList.forEach(item -> {
            map.put(item.getWeatherDateFormat(), item);
        });
        List<TbyWeatherForm> resultList = new ArrayList<>();
        for (String date : dateList) {
            String key = ToolsUtil.splitStr(date, ' ')[0];
            if (map.containsKey(key)) {
                resultList.add(map.get(key));
            } else {
                TbyWeatherForm form = new TbyWeatherForm();
                form.setWeatherDateFormat(key);
                form.setExist(false);
                resultList.add(form);
            }
        }
        return R.ok(resultList);
    }

    /**
     * 获取当前天气预报
     * @return 结果
     */
    @Mapping(value = "/today_weather", method = MethodType.GET)
    public R<TbyWeatherForm> getTodyaWeather() {
        TbyWeatherQuery query = new TbyWeatherQuery();
        query.setType("today");
        List<TbyWeatherForm> dataList = this.tbyWeatherService.list(query);
        if (dataList == null || dataList.size() == 0) {
            TbyWeatherForm form = new TbyWeatherForm();

            form.setExist(false);
            return R.ok(form);
        }
        TbyWeatherForm form = dataList.get(0);
        TbyEcologyScreenInfo screenInfo = this.tbyEcologyInfoScreenService.detailByStationId(9L);
        if (screenInfo != null) {
            form.setWindDirection(screenInfo.getWindDirectionText());
        }
        return R.ok(form);
    }
}
